package com.google.code.timeeditapplication.view.implementation;

import java.util.GregorianCalendar;
import java.util.Scanner;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.code.timeeditapplication.R;
import com.google.code.timeeditapplication.controller.IScheduleController;
import com.google.code.timeeditapplication.controller.implementation.ScheduleController;
import com.google.code.timeeditapplication.model.implementation.TimeEditCommunicator;

/**
 * This class describes the start up view, which is the first view the user sees
 * when starting the application.
 * 
 * @author antonlin
 * 
 */
public class TimeEditApplicationActivity extends Activity implements Runnable {
	private ProgressDialog dialog;
	private final IScheduleController scheduleC = ScheduleController
			.getInstance();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		final GregorianCalendar cal = new GregorianCalendar();
		this.scheduleC.setActiveDate(cal.getTime());

		setContentView(R.layout.startupview);

		Button searchHistoryButton = (Button) findViewById(R.id.startupsearchhistorybutton);
		searchHistoryButton.setEnabled(false);
	}

	private void setDateButtonText() {
		final Scanner sc = new Scanner(this.scheduleC.getActiveDate()
				.toString());

		String day, month, year;

		if (sc.hasNext()) {
			sc.next();
			month = sc.next();
			day = sc.next();
			sc.next();
			sc.next();
			year = sc.next();
		} else {
			month = "0";
			day = "0";
			year = "0";
		}
		final Button button = (Button) findViewById(R.id.startupDateButton);
		button.setText(day + " / " + month + " - " + year);
	}

	@Override
	protected void onResume() {
		super.onResume();

		setDateButtonText();
	}

	/**
	 * Initiates a loading dialog when switching to the search result view
	 * 
	 * @param v
	 * @throws InterruptedException
	 */
	public void startSearchResultView(final View v) throws InterruptedException {
		this.dialog = ProgressDialog.show(this, "", "Searching...", true);

		final Thread thread = new Thread(this);
		thread.start();
	}

	/**
	 * Switches view to the schedule day view
	 * 
	 * @param v
	 */
	public void startDayView(final View v) {
		final Intent dayViewIntent = new Intent(this, DayViewActivity.class);
		startActivity(dayViewIntent);
	}

	/**
	 * Switches view to the date chooser view
	 * 
	 * @param v
	 */
	public void startDateChooserView(final View v) {
		final Intent dateChooserIntent = new Intent(this,
				DateChooserActivity.class);
		startActivity(dateChooserIntent);
	}

	/**
	 * Switches to the search result view
	 */
	public void run() {
		final TimeEditCommunicator tEC = TimeEditCommunicator.getInstance();

		tEC.search(((EditText) findViewById(R.id.startupsearchtextfield))
				.getText().toString());

		this.dialog.dismiss();

		final Intent searchResultViewIntent = new Intent(this,
				SearchResultViewActivity.class);
		startActivity(searchResultViewIntent);
	}

	/*
	 * public void startSearchHistoryView(View v) { Intent searchHistoryIntent =
	 * new Intent(this, SearchHistoryViewActivity.class);
	 * startActivity(searchHistoryIntent); }
	 */
}